<?php

declare(strict_types=1);

namespace app\model;

use app\model\base\BaseCouponBrandTypeModel;
use think\model\concern\SoftDelete;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id 类型id主键
 * @property string $name 类型名称
 * @property int $brand_id 品牌id
 * @property bool $status 状态(1:启用,0:关闭)
 * @property int $sort 排序
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $deleted_at 删除时间,null则代表未删除
 * @property-read \app\model\CouponBrand $brand
 * @property-read \app\model\CouponProduct[] $product
 * @method static \think\db\Query onlyTrashed()
 * @method static \think\db\Query withTrashed()
 */
class CouponBrandType extends BaseCouponBrandTypeModel
{
    use SoftDelete;

    const STATUS_ACTIVE = 1; //激活
    const STATUS_CLOSE = 0; //关闭

    /**
     * 品牌类型列表
     * @params array $params
     * @return array
     */
    public function brandTypeList($params)
    {
        $type_name = isset($params['type_name'])?trim($params['type_name']):'';
        $status = isset($params['status'])?$params['status']:'';
        $brand_id = $params['brand_id'] ?? '';
        $list = self::when($type_name, function ($query) use ($type_name) {
            $query->where('name', 'like','%'.$type_name.'%');
        })->when($status != '', function ($query) use ($status) {
            $query->where('status', $status);
        })->when($brand_id, function($query) use ($brand_id){
                $query->where('brand_id', $brand_id);
        })->with(['brand' => function($query) {
            $query->withField('id,name,image_url');
        }])->order('created_at desc')
            ->withoutField('deleted_at')
            ->paginate()
            ->toArray();
        return $list;
    }

    /**
     * 新增品牌类型
     * @params int $brand_id
     */
    public function brandTypeSelect($brand_id)
    {
        return self::query()->where('brand_id', $brand_id)/*->where('status', self::STATUS_ACTIVE)*/->field(['id','name', 'sort'])->select();
    }


    /**
     * 判断是否已经存在
     * @param int $brand_id
     * @param string $name
     * @return bool
     */
    public function checkExists(int $brand_id, string $name)
    {
       return self::where('brand_id', $brand_id)->where('name', $name)->findOrEmpty();
    }

    /**
     * 一对一关联关系
     * 属于品牌
     */
    public function brand()
    {
        return $this->belongsTo(CouponBrand::class, 'brand_id', 'id');
    }

    public function product(){
        return $this->hasMany(CouponProduct::class,"type_id","id");
    }

    /**
     * 判断排序是否已存在
     * @param int $sort
     * @return bool
     */
    public function checkIsExistsSort($brand_id, $sort)
    {
        $info = self::where('brand_id', $brand_id)->where('sort', $sort)->findOrEmpty();
        if (!$info->isEmpty()) {
            return ['sort' => (int)self::max('sort')+1, 'id' => $info->id];
        }
        return ['sort' => $sort, 'id' => ''];
    }

    public function selectlinkageTypeProduct($type_id)
    {
        return CouponProduct::where('type_id', $type_id)->where('status', self::STATUS_ACTIVE)->column('name', 'id');
    }

    //某个品牌下返回所有充值类型的面额以其商品
    public function brandRechargeTypeList($params)
    {
        $type_name = isset($params['type_name'])?trim($params['type_name']):'';
        $status = isset($params['status'])?$params['status']:'';
        $brand_id = $params['brand_id'] ?? '';
        if (empty($brand_id)) {
            return [];
        }
        $list = self::when($type_name, function ($query) use ($type_name) {
            $query->where('name', 'like','%'.$type_name.'%');
        })->when($status != '', function ($query) use ($status) {
            $query->where('status', $status);
        })->when($brand_id, function($query) use ($brand_id){
            $query->where('brand_id', $brand_id);
        })->with(['brand' => function($query) {
            $query->withField('id,name,image_url');
        },'product'=>function($query) {
            $query->where('type', '<>', CouponProduct::TYPE_JTK)
                ->order('created_at desc')
                ->withOutField('mark, delete_time, type')
                ->with(['coupon_product_channel' =>function($query) {
                    $query->where('type', '<>', CouponProductChannel::TYPE_JTK)
                        ->withOutField('extend, rule, deleted_at, type, mark');
                }]);
        }])->order('created_at desc')
            ->withoutField('deleted_at')
            ->paginate()
            ->toArray();
        return $list;
    }




}
